package com.study.services;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.study.model.jdao.IOrderformDAO;
import com.study.model.mdao.DeliveryscheduleMapper;
import com.study.model.mdao.LogforcustDAO;
import com.study.model.mdao.MoneybackplanMapper;
import com.study.model.mdao.OrderFormDetailMapper;
import com.study.model.mdao.OrderFormMapper;
import com.study.model.pojo.Customer;
import com.study.model.pojo.Orderform;

/**
 * 订单表service层
 * @author wjl
 *
 */
@Service
public class OrderfromServices {
	@Autowired
	IOrderformDAO order;
	@Autowired
	OrderFormMapper orderFormMapper;
	@Autowired
	MoneybackplanMapper moneybackplanMapper;
	@Autowired
	DeliveryscheduleMapper deliveryscheduleMapper;
	@Autowired
	OrderFormMapper formMapper;
	@Autowired
	OrderFormDetailMapper detailMapper;

	@Autowired
	LogforcustDAO logforcustDAO;
	/**
	 * 判断该客户是否未完成的订单
	 * @param custid
	 * @return false 存在  true 不存在
	 */
	public boolean getfindByCust(String custname){
		boolean fig=false;
		Customer cust=new Customer();
		cust.setCusName(custname);
		System.out.println(cust.getCusName());
		List<Orderform> findByCustomer = order.findByCustomer(cust);
		if (order.findByCustomer(cust).size()>0&&findByCustomer!=null) {
			for (Orderform orderform : findByCustomer) {
				//判断是否存在未完成的订单
				if (orderform.getDictionary3().getDicId()!=418) {
					fig=false;
					break;
				}else{
					fig=true;
				}
			}
		}else{
			fig=true;
		}
		return fig;
	}
	
	/**
	 * 判断订单是否可以结束
	 * @param id
	 */
	public void statusOrder(Integer id) {
		System.out.println("开始判断当前订单");
		JSONObject findByIds = orderFormMapper.findByIds(id);
		//判断回款是否完成
		Integer findSumLv = moneybackplanMapper.findSumLv(id);
		//判断发货是否完成
		JSONObject findDeliveOk = deliveryscheduleMapper.findDeliveOk(id);
		System.err.println(findSumLv+"2222222"+findDeliveOk);
		//修改状态
		if (findDeliveOk!=null&&findSumLv==100) {
			//此种情况则代表订单结束
			formMapper.updateOrderOk(id);	
			logforcustDAO.insert(findByIds.getInteger("cus_id"), new Date(),  findByIds.getInteger("use_id"), 2203,findByIds.getInteger("o_id") +"订单结束。", "");
			
		}else  if (findSumLv==100) {
			//回款完毕
			String dos =findByIds.getInteger("o_id") +"订单回款完毕。";
			//System.out.println("订单信息如下："+findByIds);
			JSONObject findLogs = moneybackplanMapper.findLogs(dos);
			if (findLogs==null) {
				logforcustDAO.insert(findByIds.getInteger("cus_id"), new Date(),  findByIds.getInteger("use_id"), 2203,findByIds.getInteger("o_id") +"订单回款完毕。", "");
			}
			
		}else  if (findDeliveOk!=null) {
			//发货完毕
			String dos =findByIds.getInteger("o_id") +"订单发货完毕。";
			//System.out.println("订单信息如下："+findByIds);
			JSONObject findLogs = moneybackplanMapper.findLogs(dos);
			if (findLogs==null) {
				logforcustDAO.insert(findByIds.getInteger("cus_id"), new Date(),  findByIds.getInteger("use_id"), 2203,findByIds.getInteger("o_id") +"订单发货完毕。", "");
			}
			
		}
	}
	
	/**
	 * 退货商品
	 * @param custid
	 * @param orderid
	 * @param gid
	 * @param users
	 * @param quatity
	 * @param havetime
	 * @param doback
	 * @param desc
	 */
	public void addReturnGoods(String custid, String orderid, String gid, String users, String quatity,
			Date havetime, String doback, String desc) {
		JSONObject findTText = detailMapper.findTText(Integer.parseInt(orderid), Integer.parseInt(gid));
		formMapper.addReturnGoodsItem(gid, orderid, quatity, findTText.getString("o_id"), doback, desc, users, havetime);
	}

	/**
	 * 查询对应id的订单信息
	 * @param id
	 */
	public JSONObject findOrderByID(Integer id) {
		// TODO Auto-generated method stub
		return formMapper.findByIds(id);
	}
	
}
